<template>
  <div class="login">
    <el-card class="loginView">
      <div class="content" style="width: 100%">
        <img src="@/assets/logo/logo.png" style="width: 132px;"/>
        <div class="title">{{ title }}</div>
      </div>
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号">
            <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" type="password" auto-complete="off" placeholder="密码"
                    @keyup.enter.native="handleLogin">
            <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
          </el-input>
        </el-form-item>
        <el-form-item prop="code" v-if="captchaOnOff">
          <el-input v-model="loginForm.code" auto-complete="off" placeholder="验证码" @keyup.enter.native="handleLogin">
            <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
          </el-input>
          <div class="login-code">
            <img :src="codeUrl" @click="getCode" class="login-code-img" />
          </div>
        </el-form-item>

        <el-form-item>
          <el-button :loading="loading" size="medium" type="primary" class="button" @click.native.prevent="handleLogin">
            <span v-if="!loading">登 录</span>
            <span v-else>登 录 中...</span>
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <div class="bottomInfo">上海中船船舶设计技术国家工程研究中心有限公司 ©版权所有</div>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      loginForm: {
        username: "",
        password: "",
        //username: "admin",
        //password: "admin123",
        code: "",
        uu1id: ""
      },
      title:process.env.VUE_APP_TITLE,
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 验证码开关
      captchaOnOff: true,
      // 注册开关
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaOnOff = res.captchaOnOff === undefined ? true : res.captchaOnOff;
        if (this.captchaOnOff) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password)
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;

          Cookies.remove("username")
          Cookies.remove("password")
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({ path: "/index" }).catch(()=>{})
          }).catch(() => {
            this.loading = false
            if (this.captchaOnOff) {
              this.getCode()
            }
          })
        }
      })
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.bottomInfo{
  position: fixed;
  text-align: center;
  z-index: 999;
  bottom: 0px;
  font-size: 18px;
  line-height: 80px;
  color: white;
}
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  height: 100vh;
  background-image: url("../assets/images/login-background.jpg");
  background-size: cover;
  padding-bottom: 20vh;
}
.loginView{
  background-color: white;
  width: 450px;
  margin-left:auto;
  margin-right:12%;
}
.content {
  text-align: center;
  width: 327px;

  img {
    margin-bottom: 15px;
  }
}

.title {
  font-size: 30px;
  letter-spacing: 6.32px;
  margin-bottom: 20px;
}

.login-form {
  width: 286px;
  margin: 0 auto;
  .el-input {
    height: 38px;

    input {
      height: 38px;
    }
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
    color: #2fc69e;
  }
}

.login-code {
  width: 33%;
  float: right;
  position: absolute;
  top: 0;
  right: 7px;
  height: 38px;

  img {
    cursor: pointer;
    vertical-align: middle;
    border-radius: 0 5px 5px 0;
  }
}

.login-code-img {
  height: 38px;
}

.button {
  width: 100%;
  background-image: linear-gradient(-270deg, #18EAB6 3%, #12C498 100%);
  box-shadow: 0 2px 12px 0 rgba(4, 74, 56, 0.56);
  border-radius: 20px;
  border-color: #18EAB6;
  font-size: 14px;
  font-weight: bold;
  transition: all 0s;

  &:hover {
    background: #18EAB6;
    border-color: #18EAB6;
  }

  &:focus {
    background: #18EAB6;
    border-color: #18EAB6;
  }
}
</style>
